﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace AIC_Notification
{
    public class StudentStatusReportForAC : StatisticsReport
    {
        static string[] RallyStatusBkColor = new string[] { "", "#CCFFCC", "#FFFFCC", "#F8D3D3", "" };

        public enum DisplayTable { full, partial, current };

        public IList<SOF_Application> Students = null;
        public IList<RallySchoolStatistics> RallySchoolStatsList = null;
        public RallyOfRallySchoolStatistics RallyOfRallySchoolStats = null;
        public string TeacherInContext;

        private int CellWidth;
        private int MaxSchoolColumn;
        static private int PreSetColumns = 3;
        private int TotalDisplayColumn = 0;
        public int FontSize = 14;
        public string StatisticsLabel = "";

        // Report Settings
        public bool SortAscending = true;
        public bool ShowStatistics = true;
        public bool ShowURL = true;
        public DisplayTable ShowFullTable = DisplayTable.partial;
        public bool DisplayScore = true;

        public StudentStatusReportForAC(DisplayTable Display = DisplayTable.partial)
        {
            ReportTitle = "Student Status (Consultant)";
            StatisticsLabel = "P";
            ShowStatistics = true;
            ShowURL = true;
            ShowFullTable = Display;
        }

        public override void SendReportAsEmail()
        {
            foreach (var _teacher in TeacherDB.Teachers)
            {
                string HTMLBody;

                // Only for AC with one student or more
                if (_teacher.Students.Count > 0)
                {
                    Students = _teacher.Students;
                    RallySchoolStatsList = _teacher.RallySchoolStatsList;
                    RallyOfRallySchoolStats = _teacher.RallyOfRallySchoolStats;
                    TeacherInContext = _teacher.Name;
                    HeaderInformation = _teacher.Name;

                    FileName = String.Format("{0}-{1}-{2:yyyyMMdd-HHmmss}.htm", ReportTitle, _teacher.Name, Global.Now());
                    Global.SysUtil.Log("Generating Report: " + FileName);

                    HTMLBody = GetHTMLBody();

                    // Save Report to File
                    SaveReportToFile(FileName, HTMLBody);

                    // Send Report E-mail
                    toRecipients.Clear();
                    ccRecipients.Clear();

                    // Send to teacher
                    toRecipients.Add(_teacher.Email);

                    // Copy e-mail to extra recipients if not empty
                    if (!IsEmptyBody)
                    {
                    }

                    SubjectID = _teacher.Name;

                    Global.SysUtil.SendHTML(toRecipients, ccRecipients, GetSubject(), HTMLBody);
                }
            }
        }

        public override string GenerateLedger()
        {
            string htmlLedger = "";

            htmlLedger += "<tr><td>";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\"><b><u>School</u></b>: <span style=\"background-color: yellow;\">Yellow</span> = Early Application; <b>E or E2</b> = Early Application</div>";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\"><b><u>Student Essay Status</u></b>:<b>/R</b> = Has Revision; <b>/F</b> = Finalized</div> ";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\">";
            htmlLedger += "<b><u>Completion</u></b>: <b>NS</b> = Not Started; <b>IP</b> = In Progress; <b>CP</b> = Completed;  ";
            htmlLedger += String.Format("<span style=\"background-color: {0};\">Red</span> = Below Rally Goal;", RallyStatusBkColor[3]);
            htmlLedger += String.Format("<span style=\"background-color: {0};\">Yellow</span> = Close to Rally Goal;", RallyStatusBkColor[2]);
            htmlLedger += String.Format("<span style=\"background-color: {0};\">Green</span> = Met Rally Goal", RallyStatusBkColor[1]);
            htmlLedger += "</div>";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\">";
            htmlLedger += "<b><u>Essay</u></b>: <b>Avg</b>= Average # of Draft, <b>Min/Max</b> = Minimum and Maximum # of Draft";
            htmlLedger += "</div>";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\">";
            htmlLedger += "<b><u>Completion Calculation</u></b>: <b>CEF</b>= only CER is counted, not CEF; <b>HAL</b> = Only HAL is counted, not MAJ.";
            htmlLedger += "</div>";
            htmlLedger += "</td></tr>";

            return htmlLedger; 
        }

        public override string GetSubject()
        {
            return String.Format("{0} - {1} - {2:d}", ReportTitle, SubjectID, Global.Now());
        }


        private string RallyColorString(int Status)
        {
            if (Status >= 1 && Status <= 3) 
                return String.Format("background-color: {0};", RallyStatusBkColor[Status]);
            else
                return "";
        }

        public string InsertColumn(string SchoolName, int Rally, SOF_WorkList School, bool IsEDAccepted = false)
        {
            string tableColumnHeader = "";
            string tableColumn = "";

            if (ShowFullTable == DisplayTable.current)
            {
                if (Rally != RallyHelper.CurrentRally)
                    return "";
            }
            else if (ShowFullTable == DisplayTable.partial)
            {
                if (RallyHelper.CurrentRally <= 3) // Before Rally 3, show 2 previous Rally.
                {
                    if ((Rally < (RallyHelper.CurrentRally - 2)) || (Rally > RallyHelper.CurrentRally))
                        return "";
                }
                else
                {
                    if ((Rally < (RallyHelper.CurrentRally - 2)) || (Rally > RallyHelper.CurrentRally))
                        return "";
                }
            }
            else if (ShowFullTable == DisplayTable.full)
            {
                if (Rally > RallyHelper.CurrentRally)
                    return "";
            }

            if ((School != null) && (!School.NonSuppliment))
            {
                if (SchoolName != "PS" &&
                    SchoolName != "CEF" &&
                    SchoolName != "HAL")
                {
                    if (SchoolName.StartsWith("UC-", StringComparison.CurrentCultureIgnoreCase))
                        SchoolName = "UC";

                    string EncodedSchoolName = WebUtility.HtmlEncode(SchoolName);

                    if (School.ApplicationType == SOF_WorkList.ApplicationSchoolType.Early1)
                        tableColumn += String.Format("<div style=\"background-color: yellow; font-size: {1}px;\"><u>{0} - E</u></div>", EncodedSchoolName, FontSize);
                    else if (School.ApplicationType == SOF_WorkList.ApplicationSchoolType.Early2)
                        tableColumn += String.Format("<div style=\"background-color: #FFE4B5; font-size: {1}px;\"><u>{0} - E2</u></div>", EncodedSchoolName, FontSize);
                    else
                        tableColumn += String.Format("<div style=\"font-size: {1}px;\"><u>{0}</u></div>", EncodedSchoolName, FontSize);
                }

                // Insert Late Essay
                if (School.SchoolStats.NumberOfLateEssay > 0)
                {
                    tableColumn += String.Format("<div><div style=\"font-size: {1}px;\">Late Essay: {0}</div></div>", School.SchoolStats.NumberOfLateEssay, FontSize - 2);
                }

                if (RallyHelper.CurrentRally >= 4 && IsEDAccepted)
                {
                    tableColumnHeader += String.Format("<td style=\"padding: 2px 5px 2px 5px; background-color: {0};\"><div>", RallyStatusBkColor[1]);
                }
                else
                {
                    if (School.SchoolStats.RallyStatus >= 1 && School.SchoolStats.RallyStatus <= 3)
                        tableColumnHeader += String.Format("<td style=\"padding: 2px 5px 2px 5px; background-color: {0};\"><div>", RallyStatusBkColor[School.SchoolStats.RallyStatus]);
                    else
                        tableColumnHeader += "<td style=\"padding: 2px 5px 2px 5px;\"><div>";
                }

                foreach (var EssayStatistics in School.SchoolStats.EssayStatisticsList)
                {
                    string EssayName = "";
                    string EssayURL = EssayStatistics.DraftObject != null ? EssayStatistics.DraftObject.Url : "";
                    string EssayColoringHTML = "";
                    // string OverDueDraftHTML = EssayStatistics.HasOverDuedDraft ? OverDueDraftMarker : "";
                    string HasRevisionHTML = EssayStatistics.HasRevision ? "/R" : "";
                    string HasFinalHTML = EssayStatistics.IsFinal ? "/F" : "";

                    if (RallyHelper.CurrentRally >= 4 && IsEDAccepted)
                        EssayColoringHTML = RallyColorString(1);
                    else
                        EssayColoringHTML = (School.SchoolStats.RallyStatus == 4) ? RallyColorString(EssayStatistics.RallyStatus) : "";

                    if (EssayStatistics.LatestDraft == 0)
                        continue;

                    if (SchoolName == "CEF")
                    {
                        if (EssayStatistics.EssayNumber == 1)
                        {
                            EssayName = String.Format("CEF-D{0}{1}{2}", EssayStatistics.LatestDraft, HasRevisionHTML, HasFinalHTML);
                            EssayColoringHTML = "";
                        }
                        else if (EssayStatistics.EssayNumber == 2)
                        {
                            EssayName = "CER";
                            EssayColoringHTML = "";
                        }
                    }
                    else if (SchoolName == "PS")
                    {
                        EssayName = String.Format("T{0}-D{1}{2}{3}", EssayStatistics.EssayNumber, EssayStatistics.LatestDraft, HasRevisionHTML, HasFinalHTML);
                    }
                    else if (SchoolName == "HAL")
                    {
                        if (EssayStatistics.EssayNumber == 1 && EssayStatistics.LatestDraft == 0) // No MAJ
                            continue;

                        EssayName = String.Format("{0}D{1}{2}{3}", EssayStatistics.EssayNumber == 1 ? "MAJ-" : "", EssayStatistics.LatestDraft, HasRevisionHTML, HasFinalHTML);
                    }
                    else
                    {
                        if (EssayStatistics.LatestDraft < 0)
                        {
                            bool Brainstorm = true;
                        }

                        if (EssayStatistics.LatestDraft != 0)
                        {
                            EssayName = String.Format("E{0}-{1}D{2}{3}{4}",
                                EssayStatistics.EssayNumber,
                                EssayStatistics.LatestDraft < 0 ? "BS-" : "",
                                EssayStatistics.LatestDraft < 0 ? EssayStatistics.LatestDraft + 50 : EssayStatistics.LatestDraft,
                                HasRevisionHTML,
                                HasFinalHTML);
                        }
                    }

                    tableColumn += String.Format("<div style=\"font-size: {0}px; {1}\">{2}</div>",
                        FontSize,
                        EssayColoringHTML,
                        (ShowURL && EssayURL != "") ? String.Format("<a href=\"{1}\">{0}</a>", EssayName, EssayURL) : EssayName);
                }
            }

            // If Rally began and not started, mark Red
            if ((Rally <= RallyHelper.CurrentRally) && (School != null && School.SchoolStats.RallyStatus == -1))
            {
                if (SchoolName != "CEF")
                {
                    if (RallyHelper.CurrentRally >= 4 && IsEDAccepted)
                        tableColumnHeader = String.Format("<td style=\"padding: 2px 5px 2px 5px; background-color: {0};\"><div>", RallyStatusBkColor[1]);
                    else
                        tableColumnHeader = String.Format("<td style=\"padding: 2px 5px 2px 5px; background-color: {0};\"><div>", RallyStatusBkColor[3]);
                }
            }

            if (tableColumnHeader != "")
                return tableColumnHeader + tableColumn + "</div></td>";
            else
                return "";
        }

        public override string InnerTableHeader()
        {
            string tableHeader = "";
            string HeaderFormat = "<th style=\"background-color: #483D8B; color: white; font-size: {3}px; width: {0}%;\">R{2} - School {1}</th>";
            int TargetRally = 0;

            CellWidth = Math.Abs((100 - 10) / TotalDisplayColumn);

            tableHeader += "<tr>";

            // Add Student Name column
            tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {1}px; width: {0}%;\">Students</th>", CellWidth + (100 - (CellWidth * TotalDisplayColumn)), FontSize);
            if (DisplayScore)
            {
                tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {1}px; width: {0}%;\">Score</th>", 8, FontSize);
                tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {1}px; width: {0}%;\">SAT2 and AP</th>", 8, FontSize);
            }

            // Add Special List column
            if ((ShowFullTable == DisplayTable.full) || // All
                (ShowFullTable == DisplayTable.partial && (RallyHelper.CurrentRally == 1 || RallyHelper.CurrentRally == 2 || RallyHelper.CurrentRally == 3)) || // Past, Current, Next Rally
                (ShowFullTable == DisplayTable.current && RallyHelper.CurrentRally == 1)) // Current Only
            {
                tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {2}px; width: {0}%;\">{1}</th>", CellWidth, "CEF", FontSize);
                tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {2}px; width: {0}%;\">{1}</th>", CellWidth, "PS-R1", FontSize);
                tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {2}px; width: {0}%;\">{1}</th>", CellWidth, "HAL-R1", FontSize);
            }

            // Add school column header (minus CEF, PS, HAL)
            if (ShowFullTable == DisplayTable.full)
            {
                for (int i = 1; i <= MaxSchoolColumn; i++)
                {
                    TargetRally = (i + PreSetColumns) / 2;

                    if (TargetRally > RallyHelper.NoOfRally)
                        TargetRally = RallyHelper.NoOfRally;

                    if (TargetRally <= RallyHelper.CurrentRally)
                        tableHeader += String.Format(HeaderFormat, CellWidth, ((i + 1) % 2) + 1, TargetRally, FontSize);
                }
            }
            else if (ShowFullTable == DisplayTable.partial)
            {
                if (RallyHelper.CurrentRally == 1)
                {
                    //tableHeader += String.Format(HeaderFormat, CellWidth, 1, 2, FontSize);
                    //tableHeader += String.Format(HeaderFormat, CellWidth, 2, 2, FontSize);
                }
                else if (RallyHelper.CurrentRally == 2)
                {
                    tableHeader += String.Format(HeaderFormat, CellWidth, 1, RallyHelper.CurrentRally, FontSize);
                    tableHeader += String.Format(HeaderFormat, CellWidth, 2, RallyHelper.CurrentRally, FontSize);
                }
                else if (RallyHelper.CurrentRally == 3)
                {
                    tableHeader += String.Format(HeaderFormat, CellWidth, 1, RallyHelper.CurrentRally - 1, FontSize);
                    tableHeader += String.Format(HeaderFormat, CellWidth, 2, RallyHelper.CurrentRally - 1, FontSize);
                    tableHeader += String.Format(HeaderFormat, CellWidth, 1, RallyHelper.CurrentRally, FontSize);
                    tableHeader += String.Format(HeaderFormat, CellWidth, 2, RallyHelper.CurrentRally, FontSize);
                }
                else
                {
                    TargetRally = RallyHelper.CurrentRally;
                    tableHeader += String.Format(HeaderFormat, CellWidth, 1, RallyHelper.CurrentRally - 2, FontSize);
                    tableHeader += String.Format(HeaderFormat, CellWidth, 2, RallyHelper.CurrentRally - 2, FontSize);
                    tableHeader += String.Format(HeaderFormat, CellWidth, 1, RallyHelper.CurrentRally - 1, FontSize);
                    tableHeader += String.Format(HeaderFormat, CellWidth, 2, RallyHelper.CurrentRally - 1, FontSize);
                    tableHeader += String.Format(HeaderFormat, CellWidth, 1, RallyHelper.CurrentRally, FontSize);
                    tableHeader += String.Format(HeaderFormat, CellWidth, 2, RallyHelper.CurrentRally, FontSize);
                }
            }
            else if (ShowFullTable == DisplayTable.current)
            {
                if (RallyHelper.CurrentRally >= 2)
                {
                    tableHeader += String.Format(HeaderFormat, CellWidth, 1, RallyHelper.CurrentRally, FontSize);
                    tableHeader += String.Format(HeaderFormat, CellWidth, 2, RallyHelper.CurrentRally, FontSize);
                }
            }

            tableHeader += "</tr>";

            return tableHeader;
        }

        public string InsertRallyGoal()
        {
            string InnerTable = "";
            int RallyGoalFontSize = 11;
            int StartCol = 0;
            int EndCol = 0;

            InnerTable += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\"><u>Rally Goal</u></td>", FontSize);
            if (DisplayScore)
            {
                InnerTable += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\"></td>", FontSize);
                InnerTable += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\"></td>", FontSize);
            }

            if (ShowFullTable == DisplayTable.full)
            {
                if (RallyHelper.CurrentRally == 1)
                {
                    StartCol = 0;
                    EndCol = 3;
                }
                else if (RallyHelper.CurrentRally == 2)
                {
                    StartCol = 0;
                    EndCol = 5;
                }
                else if (RallyHelper.CurrentRally == 3)
                {
                    StartCol = 0;
                    EndCol = 7;
                }
                else
                {
                    StartCol = 0;
                    EndCol = ((RallyHelper.CurrentRally - 1) * 2) + PreSetColumns;
                }
            }
            else if (ShowFullTable == DisplayTable.partial)
            {
                if (RallyHelper.CurrentRally == 1)
                {
                    StartCol = 0;
                    EndCol = 3;
                }
                else if (RallyHelper.CurrentRally == 2)
                {
                    StartCol = 0;
                    EndCol = 5;
                }
                else if (RallyHelper.CurrentRally == 3)
                {
                    StartCol = 0;
                    EndCol = 7;
                }
                else
                {
                    StartCol = ((RallyHelper.CurrentRally - 2) * 2) - 1;
                    EndCol = StartCol + 6;
                }
            }
            else if (ShowFullTable == DisplayTable.current)
            {
                if (RallyHelper.CurrentRally == 1)
                {
                    StartCol = 0;
                    EndCol = 3;
                }
                else
                {
                    StartCol = (RallyHelper.CurrentRally * 2) - 1;
                    EndCol = StartCol + 2;
                }
            }

            for (int i = StartCol; i < EndCol; i++) // School + CEF/PS/HAL - Student Name
            {
                if (i == 0) // CEF
                    InnerTable += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\"></td>", RallyGoalFontSize);
                else if (i == 1) // PS/HAL
                {
                    InnerTable += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\">", RallyGoalFontSize);
                    InnerTable += String.Format("<div style=\"font-weight: bold;\">{0:MM/dd} - 4 Drafts</div>", RallyHelper.Rallys[0].MidDate);
                    InnerTable += String.Format("<div style=\"font-weight: bold;\">{0:MM/dd} - 8 Drafts</div>", RallyHelper.Rallys[0].EndDate);
                    InnerTable += String.Format("<div style=\"font-weight: bold;\">(for each topic)</div>");
                    InnerTable += "</td>";
                }
                else if (i == 2) // PS/HAL
                {
                    InnerTable += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\">", RallyGoalFontSize);
                    InnerTable += String.Format("<div style=\"font-weight: bold;\">{0:MM/dd} - 2 Drafts</div>", RallyHelper.Rallys[0].MidDate);
                    InnerTable += String.Format("<div style=\"font-weight: bold;\">{0:MM/dd} - 5 Drafts</div>", RallyHelper.Rallys[0].EndDate);
                    InnerTable += String.Format("<div style=\"font-weight: bold;\">(for HAL only)</div>");
                    InnerTable += "</td>";
                }
                else
                {
                    int TargetRally = (i + 1) / 2;

                    if (TargetRally > RallyHelper.NoOfRally - 1)
                        TargetRally = RallyHelper.NoOfRally - 1;

                    if (i % 2 == 1)
                    {
                        InnerTable += String.Format("<td colspan=\"2\" style=\"background-color: #B0E2FF; font-size: {0}px;\">", RallyGoalFontSize);
                        InnerTable += String.Format("<div style=\"font-weight: bold;\">{0:MM/dd} - {1} Drafts</div>", RallyHelper.Rallys[TargetRally - 1].MidDate, RallyHelper.Rallys[TargetRally - 1].MidGoal);
                        InnerTable += String.Format("<div style=\"font-weight: bold;\">{0:MM/dd} - {1} Drafts</div>", RallyHelper.Rallys[TargetRally - 1].EndDate, RallyHelper.Rallys[TargetRally - 1].EndGoal);
                        InnerTable += "</td>";
                    }
                }
            }

            return InnerTable;
        }

        private string InsertCompletionStatistics(int Rally, int SchoolNumber, 
            RallyOfRallySchoolStatistics CompanyRallyOfRallySchoolStats, 
            RallyOfRallySchoolStatistics LocalRallyOfRallySchoolStats,
            string CompanyMarker, string LocalMarker)
        {
            string InnerTableStatistics = "";

            InnerTableStatistics += "<td style=\"padding: 1px 1px 1px 1px; text-align: center;\">";
            InnerTableStatistics += "<table style=\"margin-left:auto; margin-right: auto;\">";
            InnerTableStatistics += InsertCompletionStatistics(CompanyRallyOfRallySchoolStats, Rally, SchoolNumber, FontSize, false, CompanyMarker);
            InnerTableStatistics += InsertCompletionStatistics(LocalRallyOfRallySchoolStats, Rally, SchoolNumber, FontSize, false, LocalMarker);
            InnerTableStatistics += "</table>";
            InnerTableStatistics += "</td>";

            return InnerTableStatistics;
        }

        private string InsertEssayStatistics(int Rally, int SchoolNumber,
            RallyOfRallySchoolStatistics CompanyRallyOfRallySchoolStats,
            RallyOfRallySchoolStatistics LocalRallyOfRallySchoolStats,
            string CompanyMarker, string LocalMarker)
        {
            string InnerTableStatistics = "";

            InnerTableStatistics += "<td style=\"padding: 1px 1px 1px 1px; text-align: center;\">";
            InnerTableStatistics += "<table style=\"margin-left:auto; margin-right: auto;\">";
            InnerTableStatistics += InsertEssayStatistics(CompanyRallyOfRallySchoolStats, Rally, SchoolNumber, FontSize, false, CompanyMarker);
            InnerTableStatistics += InsertEssayStatistics(LocalRallyOfRallySchoolStats, Rally, SchoolNumber, FontSize, false, LocalMarker);
            InnerTableStatistics += "</table>";
            InnerTableStatistics += "</td>";

            return InnerTableStatistics;
        }


        public string InnerTableStatistics()
        {
            string InnerTableStatistics = "";

            InnerTableStatistics += "<tr>";

            // Add Completion Stats column

            if (ShowFullTable == DisplayTable.full)
            {
                InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Completion</u></td>", FontSize);
                if (DisplayScore)
                {

                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                }

                // Add Special List column
                InnerTableStatistics += InsertCompletionStatistics(0, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                InnerTableStatistics += InsertCompletionStatistics(1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                InnerTableStatistics += InsertCompletionStatistics(1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                for (int i = 2; i <= RallyHelper.CurrentRally; i++)
                {
                    int NoOfSchools = i != RallyHelper.NoOfRally ? 2 : SOF_Application.MAX_SCHOOL_IN_RALLY;
                    for (int j = 0; j < NoOfSchools; j++)
                    {
                        InnerTableStatistics += InsertCompletionStatistics(i, j, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    } // for
                } // for

                InnerTableStatistics += "</tr>";

                InnerTableStatistics += "<tr>";

                // Add Essay Stats column
                InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Essay</u></td>", FontSize);
                if (DisplayScore)
                {
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                }

                InnerTableStatistics += InsertEssayStatistics(0, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                InnerTableStatistics += InsertEssayStatistics(1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                InnerTableStatistics += InsertEssayStatistics(1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                for (int i = 2; i <= RallyHelper.CurrentRally; i++)
                {
                    int NoOfSchools = i != RallyHelper.NoOfRally ? 2 : SOF_Application.MAX_SCHOOL_IN_RALLY;
                    for (int j = 0; j < NoOfSchools; j++)
                    {
                        InnerTableStatistics += InsertEssayStatistics(i, j, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    } // for
                } // for

                InnerTableStatistics += "</tr>";
            }
            else if (ShowFullTable == DisplayTable.partial)
            {
                if (RallyHelper.CurrentRally == 1)
                {
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Completion</u></td>", FontSize);
                    if (DisplayScore)
                    {

                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    // Add Special List column
                    InnerTableStatistics += InsertCompletionStatistics(0, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";

                    InnerTableStatistics += "<tr>";
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Essay</u></td>", FontSize);
                    if (DisplayScore)
                    {
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    InnerTableStatistics += InsertEssayStatistics(0, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";
                }
                else if (RallyHelper.CurrentRally == 2)
                {
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Completion</u></td>", FontSize);
                    if (DisplayScore)
                    {

                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    // Add Special List column
                    InnerTableStatistics += InsertCompletionStatistics(0, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(2, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(2, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";

                    InnerTableStatistics += "<tr>";
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Essay</u></td>", FontSize);
                    if (DisplayScore)
                    {
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    InnerTableStatistics += InsertEssayStatistics(0, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(2, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(2, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";

                }
                else if (RallyHelper.CurrentRally == 3)
                {
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Completion</u></td>", FontSize);
                    if (DisplayScore)
                    {
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    // Add Special List column
                    InnerTableStatistics += InsertCompletionStatistics(0, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(2, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(2, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(3, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(3, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";

                    InnerTableStatistics += "<tr>";
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Essay</u></td>", FontSize);
                    if (DisplayScore)
                    {
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    InnerTableStatistics += InsertEssayStatistics(0, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(2, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(2, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(3, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(3, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";

                }
                else
                {
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Completion</u></td>", FontSize);
                    if (DisplayScore)
                    {

                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    // Add Special List column
                    InnerTableStatistics += InsertCompletionStatistics(RallyHelper.CurrentRally - 2, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(RallyHelper.CurrentRally - 2, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(RallyHelper.CurrentRally - 1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(RallyHelper.CurrentRally - 1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(RallyHelper.CurrentRally, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(RallyHelper.CurrentRally, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";

                    InnerTableStatistics += "<tr>";
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Essay</u></td>", FontSize);
                    if (DisplayScore)
                    {
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    InnerTableStatistics += InsertEssayStatistics(RallyHelper.CurrentRally - 2, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(RallyHelper.CurrentRally - 2, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(RallyHelper.CurrentRally - 1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(RallyHelper.CurrentRally - 1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(RallyHelper.CurrentRally, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(RallyHelper.CurrentRally, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";
                }
            }
            else if (ShowFullTable == DisplayTable.current)
            {
                if (RallyHelper.CurrentRally == 1)
                {
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Completion</u></td>", FontSize);
                    if (DisplayScore)
                    {

                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    // Add Special List column
                    InnerTableStatistics += InsertCompletionStatistics(0, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";

                    InnerTableStatistics += "<tr>";
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Essay</u></td>", FontSize);
                    if (DisplayScore)
                    {
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    InnerTableStatistics += InsertEssayStatistics(0, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(1, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(1, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";
                }
                else
                {
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Completion</u></td>", FontSize);
                    if (DisplayScore)
                    {

                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    // Add Special List column
                    InnerTableStatistics += InsertCompletionStatistics(RallyHelper.CurrentRally, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertCompletionStatistics(RallyHelper.CurrentRally, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";

                    InnerTableStatistics += "<tr>";
                    InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"><u>Essay</u></td>", FontSize);
                    if (DisplayScore)
                    {
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                        InnerTableStatistics += String.Format("<td style=\"font-size: {0}px;\"></td>", FontSize);
                    }

                    InnerTableStatistics += InsertEssayStatistics(RallyHelper.CurrentRally, 0, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);
                    InnerTableStatistics += InsertEssayStatistics(RallyHelper.CurrentRally, 1, Statistics.RallyOfRallySchoolStats, RallyOfRallySchoolStats, "C", StatisticsLabel);

                    InnerTableStatistics += "</tr>";
                }
            }

            return InnerTableStatistics;
        }

        public override string EmptyInnerTable()
        {
            return "<tr><td style=\"padding: 10px 0 10px 0; text-align:center;\">No Student Status</td></tr>";
        }

        private void CalculateMaxSchoolColumn()
        {
            int FixedColumn = DisplayScore ? 3 : 1; // Student + Score
            const int EssayRallyColumn = 2; // 2 column per normal essay rally

            if (ShowFullTable == DisplayTable.full)
            {
                if (Students.Count > 0)
                {
                    var StudentsByName = Students.OrderBy(SOF_Application => SOF_Application.Name).ToList(); // Order by student Name

                    foreach (var student in StudentsByName)
                    {
                        int SchoolCount = student.NoOfSchool();

                        // Figure out max # of column for schools
                        if (SchoolCount > MaxSchoolColumn)
                            MaxSchoolColumn = SchoolCount;
                    }
                }

                if (RallyHelper.CurrentRally < RallyHelper.NoOfRally)
                    TotalDisplayColumn = ((RallyHelper.CurrentRally - 1) * 2) + PreSetColumns + FixedColumn;
                else
                    TotalDisplayColumn = MaxSchoolColumn + PreSetColumns + FixedColumn;
            }
            else if (ShowFullTable == DisplayTable.current)
            {
                if (RallyHelper.CurrentRally == 1)
                    TotalDisplayColumn = PreSetColumns + FixedColumn;
                else
                    TotalDisplayColumn = EssayRallyColumn + FixedColumn;
            }
            else if (ShowFullTable == DisplayTable.partial)
            {
                if (RallyHelper.CurrentRally == 1)
                    TotalDisplayColumn = PreSetColumns + FixedColumn;
                else if (RallyHelper.CurrentRally == 2)
                    TotalDisplayColumn = PreSetColumns + EssayRallyColumn + FixedColumn;
                else if (RallyHelper.CurrentRally == 3)
                    TotalDisplayColumn = PreSetColumns + EssayRallyColumn + EssayRallyColumn + FixedColumn;
                else
                    TotalDisplayColumn = (3 * EssayRallyColumn) + FixedColumn;
            }
        }

        public void SetWidthAndFontSize(int TotalColumn)
        {
            // Set table width %
            if (TotalColumn < 6)
                SetWidth(50);
            else if ((TotalColumn >= 6) && (TotalColumn <= 10))
                SetWidth(80);
            else if (TotalColumn > 10)
                SetWidth(100);

            // Set Font Size
            if (TotalColumn < 6)
                FontSize = 14;
            else if (TotalColumn >= 6 && TotalColumn < 12)
                FontSize = 12;
            else if (TotalColumn >= 12 && TotalColumn < 15)
                FontSize = 11;
            else
                FontSize = 10;
        }

        public virtual string SetSortOrder(SOF_Application student)
        {
            int KeyStudent = 0;
            if (student.StudentObject != null)
            {
                KeyStudent = student.StudentObject.KeyStudent == "0" ? 1 : 0;
            }

            return String.Format("{0}-{1:00.0}-{2}", KeyStudent, student.ApplicationStats.ProgressScore, student.Name);
        }

        public override string GenerateInnerTable()
        {
            MaxSchoolColumn = 0;
            string tableBody = "";

            CalculateMaxSchoolColumn();

            SetWidthAndFontSize(TotalDisplayColumn);

            tableBody += InsertRallyGoal();

            if (ShowStatistics)
                tableBody += InnerTableStatistics();

            if (Students.Count > 0)
            {
                foreach (var student in Students)
                    student.SortOrder = SetSortOrder(student);

                IList<SOF_Application> StudentsByName = null;
                if (SortAscending)
                    StudentsByName = Students.OrderBy(SOF_Application => SOF_Application.SortOrder).ToList(); // Order by student Name
                else
                    StudentsByName = Students.OrderByDescending(SOF_Application => SOF_Application.SortOrder).ToList(); // Order by student Name

                foreach (var student in StudentsByName)
                {
                    int ColCount = 0;
                    bool IsEDAccepted = false;

                    // Figure out if student has EDed
                    IsEDAccepted = student.StudentObject != null ? student.StudentObject.IsEarlyComplete() : false;

                    // Generate row content
                    tableBody += "<tr>";
                    string ColumnHTML = "";
                    ColCount = 0;

                    tableBody += InsertStudentName(student, FontSize, student.StudentObject != null && student.StudentObject.KeyStudent == "1" ? "#FFEC8B" : "");
                    if (DisplayScore)
                    {
                        tableBody += InsertStudentScore(student, FontSize, false);
                        tableBody += InsertSAT2andAP(student, FontSize);
                    }

                    // Find CEF List
                    SOF_WorkList CEF_List = student.GetSchool("CEF");
                    ColumnHTML = InsertColumn("CEF", 1, CEF_List);
                    if (ColumnHTML != "")
                    {
                        tableBody += ColumnHTML;
                        ColCount += 1;
                    }

                    // Find PS List
                    SOF_WorkList PS_List = student.GetSchool("PS");
                    ColumnHTML = InsertColumn("PS", 1, PS_List);
                    if (ColumnHTML != "")
                    {
                        tableBody += ColumnHTML;
                        ColCount += 1;
                    }

                    // Find HAL List
                    SOF_WorkList HAL_List = student.GetSchool("HAL");
                    ColumnHTML = InsertColumn("HAL", 1, HAL_List);
                    if (ColumnHTML != "")
                    {
                        tableBody += ColumnHTML;
                        ColCount += 1;
                    }

                    // Insert Schools
                    for (int i = 2; i <= RallyHelper.CurrentRally; i++)
                    {
                        int SchoolCount = 0;
                        // int NoOfSchools = i != RallyHelper.NoOfRally ? 2 : SOF_Application.MAX_SCHOOL_IN_RALLY;

                        for (int j = 0; j < SOF_Application.MAX_SCHOOL_IN_RALLY; j++)
                        {
                            if (SchoolCount >= 2) // Displaying 2 school per Rally
                                break;

                            SOF_WorkList ApplyingSchool = student.GetSchool(i, j);

                            if ((ApplyingSchool != null) && !ApplyingSchool.NonSuppliment)
                            {
                                ColumnHTML = InsertColumn(ApplyingSchool.School, ApplyingSchool.Rally, ApplyingSchool, IsEDAccepted);
                                if (ColumnHTML != "")
                                {
                                    tableBody += ColumnHTML;
                                    SchoolCount += 1;
                                    ColCount += 1;
                                }
                            }
                        } // for

                        // Fill in empty school
                        for (int k = 0; k < 2 - SchoolCount; k++)
                        {
                            if (ShowFullTable == DisplayTable.current)
                            {
                                if (i == RallyHelper.CurrentRally)
                                {
                                    tableBody += InsertEmptyColumn(IsEDAccepted);
                                    ColCount += 1;
                                }
                            }
                            else if (ShowFullTable == DisplayTable.partial)
                            {
                                if (RallyHelper.CurrentRally <= 3) // Before Rally 3, show 2 previous Rally.
                                {
                                    if ((i >= (RallyHelper.CurrentRally - 2)) && (i <= RallyHelper.CurrentRally))
                                    {
                                        tableBody += InsertEmptyColumn(IsEDAccepted);
                                        ColCount += 1;
                                    }
                                }
                                else
                                {
                                    if ((i >= (RallyHelper.CurrentRally - 2)) && (i <= RallyHelper.CurrentRally))
                                    {
                                        tableBody += InsertEmptyColumn(IsEDAccepted);
                                        ColCount += 1;
                                    }
                                }
                            }
                            else if (ShowFullTable == DisplayTable.full)
                            {
                                if (i <= RallyHelper.CurrentRally)
                                {
                                    tableBody += InsertEmptyColumn(IsEDAccepted);
                                    ColCount += 1;
                                }
                            }
                        }

                    } // for

                    // Fill in missing column
                    int FixedColumn = DisplayScore ? 3 : 1;
                    for (int i = ColCount; i < TotalDisplayColumn - FixedColumn; i++)
                        tableBody += InsertEmptyColumn(IsEDAccepted);

                    tableBody += "</tr>";
                }
            }
            return tableBody;
        }

        private string InsertEmptyColumn(bool IsEDAccepted)
        {
            string tableBody;

            if (RallyHelper.CurrentRally >= 4 && IsEDAccepted)
                tableBody = String.Format("<td style=\"padding: 2px 5px 2px 5px; background-color: {0};\"></td>", RallyStatusBkColor[1]);
            else
                tableBody = String.Format("<td style=\"padding: 2px 5px 2px 5px; background-color: {0};\"></td>", RallyStatusBkColor[3]);

            return tableBody;
        }
    }
}
